package com.worklikedance.priceonline;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.worklikedance.util.Log;

import com.worklikedance.model.GoldData;
import com.worklikedance.model.GoldItem;

public class GoldPriceXMLHandler extends DefaultHandler {
	private ArrayList<GoldData> mGoldDataArray = null;
	private String mCompany;
	private String mCompanyUrl;
	private String mUpdateTime;
	private String mUnit;
	private GoldData mGoldData;
	private boolean mInTitle, mInUrl;

	public GoldData getmGoldData() {
		return mGoldData;
	}

	public void setmGoldData(GoldData mGoldData) {
		this.mGoldData = mGoldData;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if (localName.equals("title")) {
			mInTitle = true;
		} else if (localName.equals("url")) {
			mInUrl = true;
		} else if (localName.equals("ratelist")) {
			mUpdateTime = attributes.getValue("updated");
			mUnit = attributes.getValue("unit");
			mGoldDataArray = new ArrayList<GoldData>();
		} else if (localName.equals("city")) {
			String city = attributes.getValue("name");
			mGoldData = null;
			mGoldData = new GoldData(city, new ArrayList<GoldItem>());
		} else if (localName.equals("item")) {
			String goldTitle = attributes.getValue("type");
			double sellPrice = Double.parseDouble(attributes.getValue("sell"));
			double buyPrice = Double.parseDouble(attributes.getValue("buy"));
			GoldItem goldItem = new GoldItem(goldTitle, sellPrice, buyPrice);

			mGoldData.addGoldItem(goldItem);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		Log.v("endElement", localName);

		if (localName.equals("title")) {
			mInTitle = false;
		} else if (localName.equals("url")) {
			mInUrl = false;
		} else if (localName.equals("city")) {
			mGoldDataArray.add(mGoldData);
		} else if (localName.equals("item")) {

		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String chars = new String(ch, start, length);
		chars = chars.trim();
		if (mInTitle) {
			mCompany = chars;
			Log.i("In tilte", chars);
		} else if (mInUrl) {
			mCompanyUrl = chars;
			Log.i("In Url", chars);
		}
	}

	/*
	 * private GoldData getGoldDataFromCity(String city) { for (int i = 0; i <
	 * mGoldDataArray.size(); i++) { GoldData goldData = mGoldDataArray.get(i);
	 * if (city.equalsIgnoreCase(goldData.getCity())) { return goldData; } }
	 * return new GoldData(city, null); }
	 */

	public String getmCompany() {
		return mCompany;
	}

	public void setmCompany(String mCompany) {
		this.mCompany = mCompany;
	}

	public String getmCompanyUrl() {
		return mCompanyUrl;
	}

	public void setmCompanyUrl(String mCompanyUrl) {
		this.mCompanyUrl = mCompanyUrl;
	}

	public String getmUpdateTime() {
		return mUpdateTime;
	}

	public void setmUpdateTime(String mUpdateTime) {
		this.mUpdateTime = mUpdateTime;
	}

	public String getmUnit() {
		return mUnit;
	}

	public void setmUnit(String mUnit) {
		this.mUnit = mUnit;
	}

	public ArrayList<GoldData> getmGoldDataArray() {
		return mGoldDataArray;
	}

	public void setmGoldDataArray(ArrayList<GoldData> mGoldDataArray) {
		this.mGoldDataArray = mGoldDataArray;
	}
}
